<?php
/**
 * Created by PhpStorm.
 * User: yangwang
 * Date: 2024-04-19
 * Time: 15:17
 */

namespace App\Kuafu\Export\Service\ExportFactory;


use App\Kuafu\Export\Model\ExportDownloadModel;
use App\Kuafu\Shop\Service\ShopRecordService;
use App\Kuafu\Shop\Service\ShopStaffRecordService;
use App\Kuafu\Statistics\Service\StatisticsService;
use Illuminate\Support\Str;

class ExportShopStaffRecordListService extends BaseExportService
{
    private $localPath = '';

    public function query(): self
    {
        $params = $this->params['otherParams'] ?? [];

        $username        = (string)($params['username'] ?? '');
        $positionStatus  = (string)($params['positionStatus'] ?? '');
        $inShop          = (string)($params['inShop'] ?? '');
        $auditStatus     = (string)($params['auditStatus'] ?? '');
        $auditStartDate  = (string)($params['auditStartDate'] ?? '');
        $auditEndDate    = (string)($params['auditEndDate'] ?? '');
        $updateStartDate = (string)($params['updateStartDate'] ?? '');
        $updateEndDate   = (string)($params['updateEndDate'] ?? '');
        $shopIds         = (string)($params['shopIds'] ?? '');
        $dimCode         = (string)($params['dimCode'] ?? '');
        $sortField       = (string)($params['sortField'] ?? '');
        $sortOrder       = (string)($params['sortOrder'] ?? '');

        ini_set('memory_limit', '1G');

        $dataArr = ShopStaffRecordService::list(
            0, 0, $username, $positionStatus, $inShop,
            $auditStatus, $auditStartDate, $auditEndDate,
            $updateStartDate, $updateEndDate, $shopIds, $dimCode, '', '', true
        )['list'];

        $this->localPath = sys_get_temp_dir() . '/' . Str::random();
        $this->streamExport($dataArr, $this->localPath);

        return $this;
    }

    public function path(): string
    {
        return $this->upload($this->localPath);
    }

    public function streamExport(
        array $list,
        string $path
    ): void {

        $fileName = $this->exportDownloadModel->name;

        StatisticsService::spreadsheetExcel([
            [
                'title' => ExportDownloadModel::FILE_TYPE_TEXT[ExportDownloadModel::FILE_TYPE_WEB_SHOP_STAFF_RECORD_LIST],
                'info'  => [],
                'rows'  => self::buildRows($list),//
            ]
        ], $fileName, $path);
    }

    public static function buildRows(array $list)
    {
        $row[] = [
            '店员姓名',
            '需要到店',
            '审核状态',
            '驳回原因',
            '健康证有效期',
            '健康证状态',
            '身份证',
            '在职状态',
            '人员权限',
            '最近审核时间',
            '商家更新时间',
            '所属门店编号',
            '所属门店名称',
            '所属门店状态',
            '所属门店督导',
            '所属门店到店员工数',
            '所属战区',
            '所属省',
            '所属市',
            '所属区',
        ];
        foreach ($list as $item) {
            $row[] = [
                $item['username'],
                $item['inShopText'],
                $item['auditStatusText'],
                $item['causeOfRejection'],
                $item['healthCardEndDate'],
                $item['healthCardStatusText'],
                $item['idCardStatusText'],
                $item['positionStatusText'],
                $item['permission'],
                $item['lastAuditTime'],
                $item['lastManagerUpdateTime'],
                $item['shopCode'],
                $item['shopName'],
                $item['shopStatusText'],
                $item['councilorName'],
                $item['inShopStaffCount'],
                $item['warZone'],
                $item['provinceName'],
                $item['cityName'],
                $item['districtName'],
            ];
        }
        return $row;
    }

}
